/*
 * Copyright (C) 2008 Universidade Federal de Campina Grande
 *  
 * This file is part of OurGrid. 
 *
 * OurGrid is free software: you can redistribute it and/or modify it under the
 * terms of the GNU Lesser General Public License as published by the Free 
 * Software Foundation, either version 3 of the License, or (at your option) 
 * any later version. 
 * 
 * This program is distributed in the hope that it will be useful, but WITHOUT 
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
 * for more details. 
 * 
 * You should have received a copy of the GNU Lesser General Public License 
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 * 
 */
package org.ourgrid.acceptance.worker;

import org.junit.Before;
import org.junit.Test;
import org.ourgrid.acceptance.util.worker.Req_004_Util;
import org.ourgrid.acceptance.util.worker.Req_094_Util;
import org.ourgrid.acceptance.util.worker.Req_120_Util;
import org.ourgrid.common.interfaces.to.WorkerStatus;
import org.ourgrid.reqtrace.ReqTest;
import org.ourgrid.worker.WorkerComponent;
import org.ourgrid.worker.WorkerConstants;

@ReqTest(reqs="REQ123")
public class Req_123_Test extends WorkerAcceptanceTestCase {
	
	private Req_004_Util req_004_Util = new Req_004_Util(getComponentContext());
	private Req_094_Util req_094_Util = new Req_094_Util(getComponentContext());
	private Req_120_Util req_120_Util = new Req_120_Util(getComponentContext());
		
	@Before
	public void setUp() throws Exception {
		super.setUp();
	}

	/**
	 */
	@ReqTest(test=" AT-123.1", reqs="REQ122")
	@Test public void test_at_123_1_scheduleIdlenessDetectionDisabledProp() throws Exception {
		
		//Start Worker with idleness detector DISABLED
		WorkerComponent component = req_004_Util.startWorker(false, false, "", "");
		
		//Verify if the worker status is IDLE
		req_094_Util.getWorkerStatus(WorkerStatus.IDLE);
		
		//Stop worker
		req_120_Util.stopWorker(component);
		
		//Start Worker with idleness detector DISABLED
		component = req_004_Util.startWorker(false, false, "1:00-23:00", "");
		
		//Verify if the worker status is IDLE
		req_094_Util.getWorkerStatus(WorkerStatus.IDLE);
		
		//Stop worker
		req_120_Util.stopWorker(component);
		
		//Start Worker with idleness detector
		component = req_004_Util.startWorker(true, false, "", "0");
		
		//Verify if the worker status is OWNER
		req_094_Util.getWorkerStatus(WorkerStatus.OWNER);
		
		//Stop worker
		req_120_Util.stopWorker(component);
		
		//Start Worker with idleness detector
		component = req_004_Util.startWorker(true, false, "1:00-23:00", "0");
		
		//Verify if the worker status is OWNER
		req_094_Util.getWorkerStatus(WorkerStatus.OWNER);
	}
	
	@ReqTest(test=" AT-123.2", reqs="REQ122")
	@Test public void test_at_123_2_scheduleIdlenessDetectionEnabledProp() throws Exception {
		
		//Start Worker with idleness detector DISABLED
		WorkerComponent component = req_004_Util.startWorker(false, true, "", "0");
		
		//Verify if the worker status is OWNER
		req_094_Util.getWorkerStatus(WorkerStatus.OWNER);
		
		//Stop worker
		req_120_Util.stopWorker(component);
		
		//Start Worker with idleness detector DISABLED
		component = req_004_Util.startWorker(false, true, "", "0");
		
		//Run the idleness detector action
		req_004_Util.runRepeatedAction(component, null, WorkerConstants.IDLENESSDETECTOR_ACTION_NAME);
		
		//Verify if the worker status is IDLE
		req_094_Util.getWorkerStatus(WorkerStatus.IDLE);
		
		//Stop worker
		req_120_Util.stopWorker(component);
		
		//Start Worker with idleness detector ENABLED
		component = req_004_Util.startWorker(false, true, "1:00-23:00", "0");
		
		//Run the idleness detector action
		req_004_Util.runRepeatedAction(component, null, WorkerConstants.IDLENESSDETECTOR_ACTION_NAME);
		
		//Verify if the worker status is IDLE
		req_094_Util.getWorkerStatus(WorkerStatus.IDLE);
		
		//Stop worker
		req_120_Util.stopWorker(component);
		
		//Start Worker with idleness detector ENABLED
		component = req_004_Util.startWorker(false, true, "1:00-1:01", "0");
		
		//Run the idleness detector action
		req_004_Util.runRepeatedAction(component, null, WorkerConstants.IDLENESSDETECTOR_ACTION_NAME);
		
		//Verify if the worker status is OWNER
		req_094_Util.getWorkerStatus(WorkerStatus.OWNER);
		
		//Stop worker
		req_120_Util.stopWorker(component);
		
		//Start Worker with idleness detector ENABLED
		component = req_004_Util.startWorker(true, true, "", "0");
		
		//Verify if the worker status is OWNER
		req_094_Util.getWorkerStatus(WorkerStatus.OWNER);

		//Stop worker
		req_120_Util.stopWorker(component);
		
		//Start Worker with idleness detector ENABLED
		component = req_004_Util.startWorker(true, true, "1:00-1:01", "0");
		
		//Run the idleness detector action
		req_004_Util.runRepeatedAction(component, null, WorkerConstants.IDLENESSDETECTOR_ACTION_NAME);
		
		//Verify if the worker status is OWNER
		req_094_Util.getWorkerStatus(WorkerStatus.OWNER);

		//Stop worker
		req_120_Util.stopWorker(component);
		
		//Start Worker with idleness detector ENABLED
		component = req_004_Util.startWorker(true, true, "1:00-23:00", "0");
		
		//Run the idleness detector action
		req_004_Util.runRepeatedAction(component, null, WorkerConstants.IDLENESSDETECTOR_ACTION_NAME);
		
		//Verify if the worker status is IDLE
		req_094_Util.getWorkerStatus(WorkerStatus.IDLE);
	}
	
	/**
	 * @throws Exception
	 */
	@ReqTest(test=" AT-123.2", reqs="REQ122")
	@Test public void test_at_123_2_scheduleIdlenessDetectionInputValidation() throws Exception {
		
		//Start Worker with Idleness Detector Schedule Time with syntactical errors
		req_004_Util.startWorkerWithScheduleIdleness("0", "0", true, false);
		
		//Start Worker with Idleness Detector Schedule Time with syntactical errors
		req_004_Util.startWorkerWithScheduleIdleness("0:", "0", true, false);

		//Start Worker with Idleness Detector Schedule Time with syntactical errors
		req_004_Util.startWorkerWithScheduleIdleness(":0", "0", true, false);
		
		//Start Worker with Idleness Detector Schedule Time with syntactical errors
		req_004_Util.startWorkerWithScheduleIdleness("00:0", "0", true, false);
		
		//Start Worker with Idleness Detector Schedule Time with syntactical errors
		req_004_Util.startWorkerWithScheduleIdleness("0:00", "0", true, false);
		
		//Start Worker with Idleness Detector Schedule Time with syntactical errors
		req_004_Util.startWorkerWithScheduleIdleness("00:00,", "0", true, false);
		
		//Start Worker with Idleness Detector Schedule Time with syntactical errors
		req_004_Util.startWorkerWithScheduleIdleness("00:00-", "0", true, false);
		
		//Start Worker with Idleness Detector Schedule Time with syntactical errors
		req_004_Util.startWorkerWithScheduleIdleness("00:00-0", "0", true, false);
		
		//Start Worker with Idleness Detector Schedule Time with syntactical errors
		req_004_Util.startWorkerWithScheduleIdleness("00:00-0:", "0", true, false);
		
		//Start Worker with Idleness Detector Schedule Time with syntactical errors
		req_004_Util.startWorkerWithScheduleIdleness("00:00-00:", "0", true, false);
		
		//Start Worker with Idleness Detector Schedule Time with syntactical errors
		req_004_Util.startWorkerWithScheduleIdleness("00:00-00:0", "0", true, false);
		
		//Start Worker with Idleness Detector Schedule Time with syntactical errors
		req_004_Util.startWorkerWithScheduleIdleness("0:0-0:0", "0", true, false);
		
		//Start Worker with Idleness Detector Schedule Time with syntactical errors
		req_004_Util.startWorkerWithScheduleIdleness(":0-0:0", "0", true, false);
		
		//Start Worker with Idleness Detector Schedule Time with syntactical errors
		req_004_Util.startWorkerWithScheduleIdleness("0-0:0", "0", true, false);
		
		//Start Worker with Idleness Detector Schedule Time with syntactical errors
		req_004_Util.startWorkerWithScheduleIdleness("-0:0", "0", true, false);
		
		//Start Worker with Idleness Detector Schedule Time with syntactical errors
		req_004_Util.startWorkerWithScheduleIdleness("00:00-00:01,", "0", true, false);

		//Start Worker with Idleness Detector Schedule Time with syntactical errors
		req_004_Util.startWorkerWithScheduleIdleness("00:00-00:01,0", "0", true, false);
		
		//Start Worker with Idleness Detector Schedule Time with syntactical errors
		req_004_Util.startWorkerWithScheduleIdleness("00:00-00:01,00:", "0", true, false);

		//Start Worker with Idleness Detector Schedule Time with syntactical errors
		req_004_Util.startWorkerWithScheduleIdleness("00:00-00:01,00:0", "0", true, false);
		
		//Start Worker with Idleness Detector Schedule Time with syntactical errors
		req_004_Util.startWorkerWithScheduleIdleness("00:00-00:01,00:00-", "0", true, false);

		//Start Worker with Idleness Detector Schedule Time with syntactical errors
		req_004_Util.startWorkerWithScheduleIdleness("00:00-00:01,00:00-00", "0", true, false);
		
		//Start Worker with Idleness Detector Schedule Time with syntactical errors
		req_004_Util.startWorkerWithScheduleIdleness("00:00-00:01,00:00-00:", "0", true, false);
		
		//Start Worker with Idleness Detector Schedule Time with syntactical errors
		req_004_Util.startWorkerWithScheduleIdleness("00:00-00:01,00:00-00:0", "0", true, false);
		
		//Start Worker with Idleness Detector Schedule Time with syntactical errors
		req_004_Util.startWorkerWithScheduleIdleness("00:00-00:01,00:00-00:01,", "0", true, false);
		
		//Start Worker with Idleness Detector Schedule Time with invalid time
		req_004_Util.startWorkerWithScheduleIdleness("99:00-25:01", "0", false, true);
		
		//Start Worker with Idleness Detector Schedule Time with invalid time
		req_004_Util.startWorkerWithScheduleIdleness("10:00-99:01", "0", false, true);
		
		//Start Worker with Idleness Detector Schedule Time with invalid time
		req_004_Util.startWorkerWithScheduleIdleness("10:61-10:62", "0", false, true);
		
		//Start Worker with Idleness Detector Schedule Time with invalid time
		req_004_Util.startWorkerWithScheduleIdleness("10:60-10:61", "0", false, true);
		
		//Start Worker with Idleness Detector Schedule Time with invalid time
		req_004_Util.startWorkerWithScheduleIdleness("00:00-00:00", "0", false, true);
		
		//Start Worker with Idleness Detector Schedule Time with invalid time
		req_004_Util.startWorkerWithScheduleIdleness("11:00-10:00", "0", false, true);
		
		//Start Worker with Idleness Detector Schedule Time with invalid time
		req_004_Util.startWorkerWithScheduleIdleness("11:59-11:00", "0", false, true);
	}
	
}